//
//  EditOneController.m
//  EducationAdminClient
//
//  Created by KWSD on 15/12/8.
//  Copyright © 2015年 周佳兴. All rights reserved.
//

#import "EditOneController.h"
#import "NSString+InputCheck.h"
@interface EditOneController ()<UITextViewDelegate,UIAlertViewDelegate>
@property(nonatomic,retain)UILabel *titleLabel;
@property(nonatomic,retain)UITextView *titleText;
@end

@implementation EditOneController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createLeftNavBar];
    [self createRightNavBar];
    
    [self creatUI];
//    [self.titleText addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}

//-(void)LeftClick:(UIButton *)btn{
//    
//    NSString *str =[NSString stringWithFormat:@"%@",[_oneFieldDic objectForKey:@"fieldRole"]];
//  BOOL is=  [NSString witchRoleis:str withStr:_titleText.text];
//    if(is){
//    
//    [self.navigationController popViewControllerAnimated:YES];
//    [[NSNotificationCenter defaultCenter ]postNotificationName:@"text" object:_titleText.text];
//    _titleText = nil;
//    [self setAutomaticallyAdjustsScrollViewInsets:NO];
//    }else{
//        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:nil message:@"请输入正确的格式" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//        [alert show];
//    }
//    
//}


-(void)createRightNavBar{
    UIButton *rightNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightNavBtn.frame = CGRectMake(0, 0, 50, 30);
//    [rightNavBtn setBackgroundImage:[UIImage imageNamed:@"形状1-19-拷贝-4"] forState:UIControlStateNormal];
    [rightNavBtn setTitle:@"" forState:UIControlStateNormal];
    
    [rightNavBtn setImage:[UIImage imageNamed:@"编辑_"] forState:UIControlStateNormal];
    
    [rightNavBtn addTarget:self action:@selector(RightClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:rightNavBtn];
    self.navigationItem.rightBarButtonItem = item;
    
}

-(void)RightClick:(UIButton *)btn{
    
    [_titleText resignFirstResponder];
    
    NSString *str =[NSString stringWithFormat:@"%@",_oneFieldDic.fieldRole];
    BOOL is;
    if([str isEqualToString:@"4"]){
        is = [NSString validateEmail:_titleText.text];
    }
    else{
        is=  [NSString witchRoleis:str withStr:_titleText.text];
    }
    if(is){
        
        [self.navigationController popViewControllerAnimated:YES];
        
        if (self.delegate) {
            [self.delegate performSelector:@selector(receiveText:) withObject:_titleText.text];
        }else{
            [[NSNotificationCenter defaultCenter ]postNotificationName:@"text" object:_titleText.text];
        }
        _titleText = nil;
        [self setAutomaticallyAdjustsScrollViewInsets:NO];
    }else{
        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:nil message:@"请输入正确的格式" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alert show];
    }

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
}

-(void)creatUI{
    _titleText = [[UITextView alloc]init];
    [self.view addSubview:_titleText];
    [_titleText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(10);
        make.top.equalTo(self.view.mas_top).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.height.equalTo(@(100));
    }];
    if(![_dataStr isEqualToString:@"(null)"]&&_dataStr!=nil){
        _titleText.text = _dataStr;
    }
    else {
         _titleText.text = @"";
    }
    _titleText.delegate = self;
    _titleText.scrollEnabled = NO;
    _titleText.layer.borderColor =[[UIColor colorWithHex:0x00a8fd]CGColor];
    _titleText.layer.borderWidth = 1;
    _titleText.font =[UIFont systemFontOfSize:15];
    _titleText.layer.cornerRadius = 5;
    
}

//- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
//{
//    UITextView *textView = object;
//    textView.contentOffset = (CGPoint){.x = 0,.y = 0};
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_titleText resignFirstResponder];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
